package com.ruiyian.project.predict.controller;

import com.google.common.collect.Maps;
import com.ruiyian.common.core.controller.BaseController;
import com.ruiyian.common.core.domain.AjaxResult;
import com.ruiyian.common.core.domain.entity.Equipment;
import com.ruiyian.common.utils.file.FileUtils;
import com.ruiyian.project.predict.domain.RecordMsg;
import com.ruiyian.project.predict.domain.SimuMsg;
import com.ruiyian.project.predict.service.PredictService;
import com.ruiyian.project.road.domain.DirePoint;
import com.ruiyian.project.road.service.DireService;
import com.ruiyian.project.vmp.gb28181.bean.DeviceChannel;
import com.ruiyian.project.vmp.storager.IVideoManagerStorager;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author chong
 * @email 673426164@qq.com
 * @create 2021/12/14
 * Desc: 道路预测
 */
@Slf4j
@RestController
@RequestMapping("/predict")
public class PredictController extends BaseController {
    @Autowired
    private PredictService predictService;
    @Value("${simulation.readPath}")
    private String path;

    @Value("${web.pdf-path}")
    private String pdfPath;
    @Autowired
    private IVideoManagerStorager videoManagerStorager;
    @Autowired
    private DireService direService;
    /**
     * 获取页面信息
     * @return
     */
    @PostMapping("/getPageInfo")
    public AjaxResult getPageInfo(@RequestBody Long roadId) {
        Map<String, Object> data = Maps.newHashMap();
        SimuMsg simuMsg = predictService.selectNewData();
        if(simuMsg ==null){
            return AjaxResult.nothing("没有获取的文件");
        }
        try {
            List<Map<String, Object>>  flowPre = predictService.readDataFlowPre(path+"/"+simuMsg.getForecastFile().replaceAll("\\\\","/"));
            data.put("flowPre",flowPre);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        try {
            List<Map<String, Object>> congestionPoint = predictService.readDataCongestionPoint(path+"/"+simuMsg.getJamFile().replaceAll("\\\\","/"));
            data.put("congestionPoint",congestionPoint);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        try {
            List<Map<String, Object>> action = predictService.readDataAction(path+"/"+simuMsg.getActionFile().replaceAll("\\\\","/"));
            data.put("action",action);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        try {
            List<Map<String, Object>> dgList = predictService.readDataAction(path+"/"+simuMsg.getDgFile().replaceAll("\\\\","/"));
            data.put("dgList",dgList);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        try {
            List<DirePoint> direPoints = direService.queryByRoadId(roadId);
            Map<Long, List<DirePoint>> collect = direPoints.stream().collect(Collectors.groupingBy(DirePoint::getRoadDireId));
            data.put("direPoint",collect);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return AjaxResult.success(data);
    }

    /**
     * 查询仿真视频
     * @param equipment   code ifVirtual
     * @return
     */
    @PostMapping("/searchPreVideo")
    public AjaxResult searchPreVideo(@RequestBody Equipment equipment) {
        Map<String, Object> data = Maps.newHashMap();
        RecordMsg recordMsg = null;
        try {
            recordMsg = predictService.selectNewRecordDataByCode(equipment.getCode());
            // 预测视频
            if(null !=recordMsg && equipment.getEqType().equals("0")){
                data.put("afterVideo",recordMsg.getPlayFile().replaceAll("\\\\data","").replaceAll("\\\\","/"));
                data.put("RUN_ID",recordMsg.getRunId());
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        if(equipment.getEqType().equals("0")){
            try {
                if(ObjectUtils.notEqual(equipment.getIfVirtual(),1)){
                    // 实时监控
                    DeviceChannel deviceChannel = videoManagerStorager.queryByCode(equipment.getId());
                    data.put("deviceChannel",deviceChannel);
                    // 前15分钟仿真
                    if(data.size()>0){
                        Integer runId = Integer.parseInt(data.get("RUN_ID").toString());
                        RecordMsg recordMg = predictService.selectNewRecordDataByCodeAndRunId(equipment.getCode(), runId - 1);
                        if(recordMg!= null){
                            data.put("beforeVideo",recordMg.getPlayFile().replaceAll("\\\\data","").replaceAll("\\\\","/"));
                        }
                    }
                }
            } catch (Exception e) {
                log.error(e.getMessage());
            }
        }else {
            if(equipment.getEqType().equals("2")){
                // 30分钟之后
                data.put("afterVideo",recordMsg.getPlayFile().replaceAll("\\\\data","").replaceAll("\\\\","/"));
            }else if(equipment.getEqType().equals("3")){
                Integer runId = Integer.parseInt(recordMsg.getRunId());
                RecordMsg record = predictService.selectNewRecordDataByCodeAndRunId(equipment.getCode(), runId - 1);
                if(recordMsg!= null){
                    data.put("beforeVideo",record.getPlayFile().replaceAll("\\\\data","").replaceAll("\\\\","/"));
                }
            }else if(equipment.getEqType().equals("1")){
                DeviceChannel deviceChannel = videoManagerStorager.queryByCode(equipment.getId());
                data.put("deviceChannel",deviceChannel);
            }
        }
        return AjaxResult.success(data);
    }

    /**
     * 获取PDF文件
     * @return
     */
    @GetMapping("/getPdfList")
    public AjaxResult getPdfList() {
        List<String> fileList = null;
        try {
             fileList = FileUtils.getFileList(pdfPath);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return AjaxResult.success(fileList);
    }
}
